package components.utils
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.EventDispatcher;
    import flash.utils.Dictionary;

    /**
     *与对象的事件监听有关
     * @author zn
     *
     */
    public class EventListenerUtil
    {
        //key:displayObject,value:list
        private static var _displayObjectDic:Dictionary = new Dictionary();

        public static function addEventListener(object:Object, 
												type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
        {
			if(object is EventDispatcher)
			{
				object.addEventListener(type,listener,useCapture,priority,useWeakReference);
				pushListenerFunc(object,type,listener);
			}
        }

        public static function pushListenerFunc(object:Object, eventName:String, func:Function):void
        {
			if(object is EventDispatcher)
			{
	            if (_displayObjectDic[object] == null)
	                _displayObjectDic[object] = [];
	
            	_displayObjectDic[object].push({ eventName: eventName, func: func });
			}
        }

        public static function removeAllListenerFunc(object:Object):void
        {
            if (_displayObjectDic[object])
            {
                var list:Array = _displayObjectDic[object];
                var eventName:String;
                var func:Function;
				var obj:Object;
				
				while(list.length>0)
				{
					obj=list.pop();
                    eventName = obj["eventName"];
                    func =obj["func"];

                    object.removeEventListener(eventName, func);
					object.removeEventListener(eventName, func,true);
				}
                delete _displayObjectDic[object];
            }
        }
		
		public static function removeListenerFunc(obj:Object,eventName:String, func:Function):void
		{
			if (_displayObjectDic[obj])
			{
				var list:Array = _displayObjectDic[obj];
				
				for (var i:int = 0; i < list.length; i++) 
				{
					if(list[i]["eventName"]==eventName && list[i]["func"]==func)
					{
						list.splice(i,1);
						obj.removeEventListener(eventName, func);
						obj.removeEventListener(eventName, func,true);
						break;
					}
				}
				
			}
		}
    }
}
